#!/usr/bin/env python
# -*- coding:utf-8 -*-

"""
@author zyx
@since 2022/2/27 18:59
@file: c01_mysql注册登录练习.py
"""
import pymysql

# 创建链接对象：链接数据库
conn = pymysql.Connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='123321',
    db='spider3qi',
    charset='utf8'
)
# 创建游标对象：执行sql语句
cursor = conn.cursor()


# 注册函数
def regist():
    username = input('enter your username:')
    password = input('enter your password:')
    repeatpwd = input('password agin:')
    email = input('enter email:')
    # 检测两次密码是否一致
    if password == repeatpwd:
        # 检测注册的用户明是否重复
        query_sql = 'select * from users where uname="%s"' % username
        cursor.execute(query_sql)
        # 获取查询结果中的第一条数据
        query_result = cursor.fetchone()
        if query_result == None:  # 没有查询到注册的用户名存在
            # 将数据进行持久化存储
            print('恭喜，注册成功！')
            insert_sql = 'insert into users (uname,pwd,email)values("%s","%s","%s")' % (username, password, email)
            cursor.execute(insert_sql)
            conn.commit()
        else:  # 用户名重复
            print('用户名存在，注册失败！')
    else:
        print('注册失败，两次密码不一致！')
